package com.vc.commons.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.vc.commons.model.BaseResponse;

/**
 * 处理公共异常的
 * 
 * @author Cris
 *
 */
@ControllerAdvice
public class BusinessExceptionHandler {
	
	private final static Logger logger = LoggerFactory.getLogger(BusinessException.class);
	
	/**
	 * 0表示请求中出现过异常
	 */
	private final static String RESULT = "0";

	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(value = { BusinessException.class, Exception.class })
	public @ResponseBody BaseResponse<?> jsonErrorHandler(HttpServletRequest request,BusinessException businessException,Exception exception) {
		logger.error(exception.getMessage(),exception);
		exception.printStackTrace();
		BaseResponse<Object> baseResponse = new BaseResponse<Object>();
		baseResponse.setResult(RESULT);
		baseResponse.setErrorMsg(exception.getMessage());
		return baseResponse;
	}

}
